package com.zyd.controller.power;

import com.zyd.constants.ErrorDef;
import com.zyd.io.vo.power.SavePowerVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.service.power.PowerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/powers")
public class PowersController {

    private final static Logger LOGGER = LoggerFactory.getLogger(PowersController.class);

    @Autowired
    private PowerService service;

    @PostMapping(value = "findPowersByRoleid", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findPowers(@RequestBody Map<String, Object> map) {
        try {
            ResBody resBody =  new ResBody(ErrorDef.SUCCESS, "查询权限列表成功");
            Map<String, Object> ret = service.queryPowsersByRoleId((String) map.get("roleid"));
            resBody.setData(ret);
            return resBody;
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询权限列表失败");
        }
    }


    @PostMapping(value = "savePowers", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody savePowers(@RequestBody SavePowerVO request) {
        try {
            ResBody resBody =  new ResBody(ErrorDef.SUCCESS, "保存权限列表成功");
            service.savePowers(request);
            return resBody;
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "保存权限列表失败");
        }
    }

    @PostMapping(value = "findPowersByUserid", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findPowersByUserid(@RequestBody Map<String, Object> map) {
        try {
            ResBody resBody =  new ResBody(ErrorDef.SUCCESS, "根据用户ID查询权限列表成功");
            Map<String, Object> ret = service.queryPowsersByUserId((String) map.get("userid"));
            resBody.setData(ret);
            return resBody;
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "根据用户ID查询权限列表失败");
        }
    }
    //获取权限地址
    @PostMapping(value = "getLocation", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody getLocation() {
        try {
            ResBody resBody =  new ResBody(ErrorDef.SUCCESS, "根据用户ID查询地区权限列表成功");
            String location = service.getLocation();
            resBody.setData(location);
            return resBody;
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "根据用户ID查询地区权限列表失败");
        }
    }
}
